<?php
namespace Home\Controller;
use Think\Controller;
// +----------------------------------------------------------------------
// | Date:2017年9月8日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class DataController extends Controller{
    
    /**
     * 数据发送
     * 
     * 迁移过程的辅助程序
     * 需要复制备份 checkinout 为CHECKINOUT_BAK
     * 然后清空checkinout ，因为表的主键做了调整，这个时候需要拉取旧数据
     * 
     * date:2017年9月8日
     * author: EK_熊
     */
    public function index(){
        $num = 5000;//默认记录数
        $la_userid = 5;
        $la_checktime = "2017-09-08 00:00:00.000";//从哪一天开始拉取
        
        $count = 0;//计数器
        $start = date('Y-m-d',strtotime($la_checktime));
        $this->dump("是否要从{$start}这一天开始拉取数据？[Y/N]");
        $answer = trim(fgets(STDIN));
        if (strtoupper($answer) == 'N') exit();
        
        $this->dump("是否已经清空数据表？[Y/N]");
        $answer = trim(fgets(STDIN));
        if (strtoupper($answer) == 'N') exit();
        
        $checkbakModel = D('checkinoutBak');
        $checkModel = D('checkinout');
        do{
            if ($la_userid > 0 && $la_checktime >0 ) {
                $map = ['userid'=>$la_userid,'CHECKTIME'=>['gt',$la_checktime]];
            }
            $list = $checkbakModel->where($map)->limit($num)->order('CHECKTIME')->field('id',true)->select();
            if ($list) {
                $list_num = count($list);
                $lastary = $list[$list_num-1];
    
                $la_userid = $lastary['userid'];
                $la_checktime = $lastary['checktime'];
                //添加数据
                for($i=0;$i<$list_num;$i++){
                    $data = [
                        'USERID'=>$list[$i]['userid'],
                        'CHECKTIME'=>$list[$i]['checktime'],
                        'VERIFYCODE'=>$list[$i]['verifycode'],
                        'SENSORID'=>$list[$i]['sensorid'],
                        'Memoinfo'=>$list[$i]['memoinfo'],
                        'WorkCode'=>$list[$i]['workcode'],
                        'UserExtFmt'=>$list[$i]['userextfmt'],
                        'sn'=>$list[$i]['sn'],
                    ];
                    $a = $checkModel->add($data);
                }
                $count = $count+$list_num;
                $this->dump("累计成功导入{$count}条记录");
            }
            
        }while($list && IS_CLI);

        $this->dump('导入完毕！！');
        
    }
        
    public function dump($str){
        if (IS_CLI) {
            echo iconv('UTF-8', 'GBK', $str)."\r\n";
        }else{
            dump($str);
        }
    
    }
    
    
    public function test(){
        
        dump(D('OriginalCheckData'));
    }
    
}